//@author A0121492M
public class AddCommand extends Command {
	public Task task = null;
	public int taskid = -1;
	public boolean isGuiOpenCommand = false;

	public ExecutionReply execute()
			throws PopupClosedException {
		this.taskid = findNextTaskID();
		Task addtask = new Task();
		if (this.isGuiOpenCommand == false) {
			this.task.id = this.taskid;
			addtask = this.task;
			addTask(addtask);
		} else if (this.isGuiOpenCommand == true) {
			addtask.id = this.taskid;
			addtask = PopUps.executeAdd(addtask);
			Logic.checkIfPopupClosed(addtask);
			addTask(addtask);
		}
		SingleTaskReply s = new SingleTaskReply();
		s.task = addtask;
		Logic.saveData();
		Logic.updateCommandHistory(this);
		return s;
	}
	
	public void undo() {
		DeleteCommand delete = new DeleteCommand();
		delete.taskId = this.taskid;
		delete.redo();
	}
	
	public void redo() {
		addTask(this.task);
		Logic.saveData();
	}
	
	private static void addTask(Task task) {
		Logic.loadedData.add(task);
	}
	
	private static int findNextTaskID() {
		int greatestID = 1;
		if (Logic.loadedData.isEmpty()) {
			return greatestID;
		} else {
			for (int i = 0; i < Logic.loadedData.size(); i++) {
				if (greatestID < Logic.loadedData.get(i).id) {
					greatestID = Logic.loadedData.get(i).id;
				}
			}
			return greatestID + 1;
		}
	}
}